package org.jeecg.modules.arc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.arc.entity.BaseSubWaterPrices;
import org.jeecg.modules.arc.entity.BaseWaterPrices;
import org.jeecg.modules.arc.mapper.BaseSubWaterPricesMapper;
import org.jeecg.modules.arc.mapper.BaseWaterPricesMapper;
import org.jeecg.modules.arc.service.IBaseWaterPricesService;
import org.jeecg.modules.arc.vo.BaseWaterPricesVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 用水价格
 * @Author: jeecg-boot
 * @Date:   2022-02-13
 * @Version: V1.0
 */

@Service("baseWaterPricesServiceImpl")
public class BaseWaterPricesServiceImpl extends ServiceImpl<BaseWaterPricesMapper, BaseWaterPrices> implements IBaseWaterPricesService {

	@Resource
	private BaseWaterPricesMapper baseWaterPricesMapper;
	@Resource
	private BaseSubWaterPricesMapper baseSubWaterPricesMapper;

	@Override
	@Transactional
	public void saveMain(BaseWaterPrices baseWaterPrices, List<BaseSubWaterPrices> baseSubWaterPricesList) {
		baseWaterPricesMapper.insert(baseWaterPrices);
		if(baseSubWaterPricesList!=null && baseSubWaterPricesList.size()>0) {
			for(BaseSubWaterPrices entity:baseSubWaterPricesList) {
				//外键设置
				entity.setPriceId(baseWaterPrices.getId());
				baseSubWaterPricesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(BaseWaterPrices baseWaterPrices,List<BaseSubWaterPrices> baseSubWaterPricesList) {
		baseWaterPricesMapper.updateById(baseWaterPrices);

		//1.先删除子表数据
		baseSubWaterPricesMapper.deleteByMainId(baseWaterPrices.getId());

		//2.子表数据重新插入
		if(baseSubWaterPricesList!=null && baseSubWaterPricesList.size()>0) {
			for(BaseSubWaterPrices entity:baseSubWaterPricesList) {
				//外键设置
				entity.setPriceId(baseWaterPrices.getId());
				baseSubWaterPricesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		baseSubWaterPricesMapper.deleteByMainId(id);
		baseWaterPricesMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			baseSubWaterPricesMapper.deleteByMainId(id.toString());
			baseWaterPricesMapper.deleteById(id);
		}
	}

	@Override
	public List<BaseWaterPricesVo> queryWaterPriceByNatureId(String natureId,String isAlien) {
		return baseWaterPricesMapper.queryWaterPriceByNatureId(natureId,isAlien);
	}

}
